const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsName: "", //搜索关键字
    goodsType: 0, //0普通 2推荐 3热门 4新品 5 折扣
    sortType: 0, //排序
    treeId:'',
    page: 1,
    goodsList: [],
    loadingStatus: false,
    msgStatus: false
  },
  // 排序
  sortList(e) {
    let that = this;
    let sortType = e.target.dataset.type;
    let thatSortType = that.data.sortType;
    that.setData({
      msgStatus: false
    })
    if (sortType == 0 || sortType == 1) {
      if (sortType == thatSortType) {
        wx.hideLoading();
        return;
      } else {
        that.setData({
          sortType: sortType
        })
      }
    } else if (sortType == 2) {
      if (thatSortType == 2) {
        that.setData({
          sortType: 3
        })
      } else {
        that.setData({
          sortType: 2
        })
      }
    }
    wx.showLoading({
      title: '排序中...',
    })
    wx.request({
      url: app.buildUrl('/shop/supplier-goods/get-goods-list'),
      header: app.buildHeader(),
      data: {
        page:1,
        goods_name: that.data.goodsName,
        goods_type: that.data.goodsType,
        sort_type: that.data.sortType,
        type: 1,
        tree_id: that.data.treeId
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          let newList = res.data.data;
          newList.forEach(function (i) {
            i.max_discount_price = parseFloat(i.max_discount_price).toFixed(2);
            i.max_original_price = parseFloat(i.max_original_price).toFixed(2);
            i.min_discount_price = parseFloat(i.min_discount_price).toFixed(2);
            i.min_original_price = parseFloat(i.min_original_price).toFixed(2);
            if (parseInt(i.display_sales) > 10000) {
              i.display_sales = i.display_sales / 10000 + 'w'
            }
          })
          that.setData({
            goodsList: newList,
            page: 1
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  // 加载更多
  load() {
    let that = this;
    if (that.data.loadingStatus) { return; }
    if (that.data.msgStatus) { return; }
    that.setData({
      loadingStatus: true
    })
    let page = that.data.page;
    page = ++page;
    wx.showLoading({
      title: '正在加载更多',
    })
    wx.request({
      url: app.buildUrl('/shop/supplier-goods/get-goods-list'),
      header: app.buildHeader(),
      data: {
        page: page,
        goods_name: that.data.goodsName,
        goods_type: that.data.goodsType,
        sort_type: that.data.sortType,
        type: 1,
        tree_id: that.data.treeId
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          if (res.data.data.length == 0) {
            app.showToast('没有更多了')
            that.setData({
              msgStatus: true
            })
          } else {
            let newList = res.data.data;
            newList.forEach(function (i) {
              i.max_discount_price = parseFloat(i.max_discount_price).toFixed(2);
              i.max_original_price = parseFloat(i.max_original_price).toFixed(2);
              i.min_discount_price = parseFloat(i.min_discount_price).toFixed(2);
              i.min_original_price = parseFloat(i.min_original_price).toFixed(2);
              if (parseInt(i.display_sales) > 10000) {
                i.display_sales = i.display_sales / 10000 + 'w'
              }
            })
            that.setData({
              goodsList: that.data.goodsList.concat(newList),
              msgStatus: false
            })
          }
        } else {
          page = --page;
          app.showToast(res.data.msg)
        }
        that.setData({
          page: page,
          loadingStatus: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.setNavigationBarTitle({
      title: options.title
    })
    that.setData({
      goodsName: options.key || "",
      goodsType: options.type || 0 ,
      treeId:options.id
    })
    wx.showLoading({
      title: '加载列表...',
    })
    wx.request({
      url: app.buildUrl('/shop/supplier-goods/get-goods-list'),
      header: app.buildHeader(),
      data: {
        page:1,
        goods_name: that.data.goodsName,
        goods_type: that.data.goodsType,
        sort_type:0,
        type: 1,
        tree_id: that.data.treeId
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          let newList = res.data.data;
          newList.forEach(function(i){
            i.max_discount_price = parseFloat(i.max_discount_price).toFixed(2);
            i.max_original_price = parseFloat(i.max_original_price).toFixed(2);
            i.min_discount_price = parseFloat(i.min_discount_price).toFixed(2);
            i.min_original_price = parseFloat(i.min_original_price).toFixed(2);
            if (parseInt(i.display_sales) > 10000) {
              i.display_sales = i.display_sales / 10000 + 'w'
            }
          })
          that.setData({
            goodsList: newList
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})